package com.sam.im.heiqun.uis.dialogs;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.ImMessage;
import com.sam.im.heiqun.uis.activities.GroupNoticeListActivity;
import com.sam.im.heiqun.uis.beans.NoticeBean;
import com.yuyh.library.utils.GlideUtils;

import java.util.List;

/**
 * Created by wangguodong on 2017/1/9.
 */
public class MyAlertDialogManages {

    TextView textDetails;
    static boolean isgo = false ;

    private static OnAlertSelectId mOnAlertSelectId ;
    private static OnAlertOkSelectId mOnAlertOkSelectId ;
    private static OnAlertOkImage mOnAlertOkImage ;
    private static Context mContext ;
    public  static  Dialog dlg ;
    public interface OnAlertSelectId
    {
        void onClick(int whichButton);
    }

    public interface OnAlertOkSelectId
    {
        void onOkClick(int whichButton, String content);
    }
    public interface OnAlertOkImage
    {
        void onOkImageClick(ImageView mImageView);
    }

    private MyAlertDialogManages(){ }

    public static Dialog initShowCreatGroup(final Context context,final OnAlertOkSelectId onAlertSelectId){
        mContext = context ;
        mOnAlertOkSelectId = onAlertSelectId ;
        return ShowCreatGroup();
    }
    private static Dialog ShowCreatGroup(){
        dlg = new Dialog(mContext, R.style.MMThem_DataSheet);
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_creat_group, null);

        final int cFullFillWidth = ((Activity) mContext).getWindowManager().getDefaultDisplay().getWidth() * 5 / 6;
        layout.setMinimumWidth(cFullFillWidth);

        final EditText edit_creat_name = (EditText) layout.findViewById(R.id.edit_creat_name);
        TextView ok = (TextView) layout.findViewById(R.id.ok);
        TextView cancle = (TextView) layout.findViewById(R.id.cancle);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = edit_creat_name.getText().toString().trim();
                if (null==name || "".equals(name)){
                    if (null!=mOnAlertOkSelectId)mOnAlertOkSelectId.onOkClick(1,name);
                }else{
                    if (name.length() > 10){
                        Toast.makeText(mContext, "名称长度不能大于10", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    dlg.dismiss();
                    if (null!=mOnAlertOkSelectId)mOnAlertOkSelectId.onOkClick(0,name);
                }
            }
        });
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dlg.dismiss();
            }
        });

        // set a large value put it in bottom
        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.x = 0;
        final int cMakeBottom = 0;
        lp.y = cMakeBottom;
        lp.gravity = Gravity.CENTER;
        dlg.onWindowAttributesChanged(lp);
        dlg.setContentView(layout);
        dlg.show();

        return dlg;
    }
    public static Dialog initShowRedPacket(final Context context,String head,String name ,String beizhu ,String type,final OnAlertOkImage onAlertOkImage){
        mContext = context;
        mOnAlertOkImage = onAlertOkImage ;
        return ShowRedPacket(head,name,beizhu,type);
    }
    private static Dialog ShowRedPacket(String head ,String name,String beizhu,String type){
        dlg = new Dialog(mContext, R.style.MMThem_DataSheet);
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_red_packet_kai, null);

        final int cFullFillWidth = ((Activity) mContext).getWindowManager().getDefaultDisplay().getWidth() * 5 / 6;
        final int cFullFillHeight = ((Activity) mContext).getWindowManager().getDefaultDisplay().getHeight() * 9 / 14;
        layout.setMinimumWidth(cFullFillWidth);
        layout.setMinimumHeight(cFullFillHeight);

        TextView clean = (TextView) layout.findViewById(R.id.clean);
        clean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dlg.dismiss();
            }
        });
        ImageView user_head = (ImageView) layout.findViewById(R.id.user_head);
        GlideUtils.loadCircleImage(mContext,head,user_head);
        TextView user_name = (TextView) layout.findViewById(R.id.user_name);
        user_name.setText(name);
        TextView red_type = (TextView) layout.findViewById(R.id.red_type);
        if (null!=type ){
            if ("1".equals(type)){//拼手气
                red_type.setText(R.string.send_a_red_package);
            }else if ("2".equals(type)){//等额普通
                red_type.setText(R.string.send_a_red_package);
            }
        }

        TextView red_beizhu = (TextView) layout.findViewById(R.id.red_beizhu);
        red_beizhu.setText(beizhu);
        final ImageView red_open = (ImageView) layout.findViewById(R.id.red_open);
        red_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnAlertOkImage.onOkImageClick(red_open);
            }
        });


        // set a large value put it in bottom
        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.x = 0;
        final int cMakeBottom = 0;
        lp.y = cMakeBottom;
        lp.gravity = Gravity.CENTER;
        dlg.onWindowAttributesChanged(lp);
        dlg.setContentView(layout);
        dlg.show();

        return dlg;
    }
    public static Dialog initShowNoticed(final Context context, ImMessage imMessage , final OnAlertOkImage onAlertOkImage){
        mContext = context;
        mOnAlertOkImage = onAlertOkImage ;
//        NoticeBean noticeBean

        return ShowNoticed(imMessage);
    }

    private static Dialog ShowNoticed(final ImMessage imMessage){
        isgo = false ;
        final NoticeBean noticeBean = new Gson().fromJson(imMessage.getContent(),NoticeBean.class);
        final Dialog mdlg = new Dialog(mContext, R.style.MMThem_DataSheet);
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_notice, null);

        final int cFullFillWidth = ((Activity) mContext).getWindowManager().getDefaultDisplay().getWidth() * 5 / 6;
        final int cFullFillHeight = ((Activity) mContext).getWindowManager().getDefaultDisplay().getHeight() * 9 / 14;
        layout.setMinimumWidth(cFullFillWidth);
        layout.setMinimumHeight(cFullFillHeight);

        LinearLayout linear_more = (LinearLayout) layout.findViewById(R.id.linear_more);
        TextView txt_title = (TextView) layout.findViewById(R.id.txt_title);
        TextView txt_time = (TextView) layout.findViewById(R.id.txt_time);
        TextView txt_content = (TextView) layout.findViewById(R.id.txt_content);
        Button button_cancle = (Button) layout.findViewById(R.id.button_cancle);

        linear_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isgo){
                    isgo = true ;
                    GroupNoticeListActivity.startActivity((Activity) mContext,noticeBean.getGroupId());
                    mdlg.dismiss();
                }

            }
        });
        button_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//
                try {
                    List<ImMessage> imMessageS1 = ImMessage.findById(ImMessage.class, new String[]{""+imMessage.getId()});
                    if (null!=imMessageS1 && imMessageS1.size() >0){
                        ImMessage imMessagem = imMessageS1.get(0);
                        imMessagem.setPayed(true);
                        imMessagem.save();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

                ImMessage imMessagemm = imMessage;
                imMessagemm.setPayed(true);
                imMessagemm.save();

                mdlg.dismiss();
            }
        });

        if (null!=noticeBean){
            txt_title.setText(noticeBean.getTitle());
            txt_time.setText(noticeBean.getCreateTime());
            txt_content.setText(noticeBean.getContent());

        }



        // set a large value put it in bottom
        Window w = mdlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.x = 0;
        final int cMakeBottom = 0;
        lp.y = cMakeBottom;
        lp.gravity = Gravity.CENTER;
        mdlg.onWindowAttributesChanged(lp);
        mdlg.setContentView(layout);
        mdlg.setCanceledOnTouchOutside(false);
        mdlg.setCancelable(false);
        mdlg.show();

        return mdlg;
    }

    public static Dialog initShowImageFun(final Context context, final OnAlertOkSelectId onAlertOkSelectId){
        mContext = context;
        mOnAlertOkSelectId = onAlertOkSelectId ;
//        NoticeBean noticeBean

        return ShowImageFun();
    }

    private static Dialog ShowImageFun(){
        isgo = false ;
//        final NoticeBean noticeBean = new Gson().fromJson(imMessage.getContent(),NoticeBean.class);
        final Dialog mdlg = new Dialog(mContext, R.style.MMThem_DataSheet);
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_manager_image_fun, null);

        final int cFullFillWidth = ((Activity) mContext).getWindowManager().getDefaultDisplay().getWidth() * 5 / 6;
        final int cFullFillHeight = ((Activity) mContext).getWindowManager().getDefaultDisplay().getHeight() * 9 / 14;
        layout.setMinimumWidth(cFullFillWidth);
        layout.setMinimumHeight(cFullFillHeight);


        TextView txt_zhuan = (TextView) layout.findViewById(R.id.txt_zhuan);
        TextView txt_save = (TextView) layout.findViewById(R.id.txt_save);
        TextView txt_clean = (TextView) layout.findViewById(R.id.txt_clean);

        txt_zhuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mdlg.dismiss();
                if (null!=mOnAlertOkSelectId)mOnAlertOkSelectId.onOkClick(1, "");
            }
        });
        txt_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mdlg.dismiss();
                if (null!=mOnAlertOkSelectId)mOnAlertOkSelectId.onOkClick(2, "");
            }
        });
        txt_clean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mdlg.dismiss();
                if (null!=mOnAlertOkSelectId)mOnAlertOkSelectId.onOkClick(0, "");
            }
        });


        // set a large value put it in bottom
        Window w = mdlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.x = 0;
        final int cMakeBottom = 0;
        lp.y = cMakeBottom;
        lp.gravity = Gravity.CENTER;
        mdlg.onWindowAttributesChanged(lp);
        mdlg.setContentView(layout);
        mdlg.setCanceledOnTouchOutside(false);
        mdlg.setCancelable(false);
        mdlg.show();

        return mdlg;
    }



}
